package adapter.user_management;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;
import define.API;
import model.user_management.AllTabInUserManagementItem;

/**
 * Created by trek2000 on 17/10/2014.
 */
public class GeneralUserTabInUserManagementFragmentAdapter extends BaseAdapter {

    /**
     * Data section
     */
    private ArrayList <AllTabInUserManagementItem> mAlGeneralUserTabInUserManagement = new ArrayList<>();

    /**
     * String section
     */
    private int textViewResourceId;
    private DisplayImageOptions mDio;
    private ViewHolder viewHolder;
    /**
     * The other section
     */
    private Context mContext;

    private class ViewHolder {
        private CircleImageView circleIvItem;
        private ImageButton mIbtnRemoveUser;
        private ImageView mIvPhoto;
        private ImageView mIvVideo;
        private LinearLayout mLLPhotosAndVideos;
        private LinearLayout mLLPendingUser;
        private TextView mTvMemberName;
        private TextView mTvPhotoNumber;
        private TextView mTvVideoNumber;
    }

    public GeneralUserTabInUserManagementFragmentAdapter(Context context, int textViewResourceId, ArrayList<AllTabInUserManagementItem> mAlgeneralUserTabInUserManagement) {
        this.mContext = context;
        this.textViewResourceId = textViewResourceId;
        this.mAlGeneralUserTabInUserManagement = mAlgeneralUserTabInUserManagement;

        mDio = new DisplayImageOptions.Builder()
                .showImageOnLoading(mContext.getResources().getColor(R.color.black))
                .delayBeforeLoading(1000)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    @Override
    public int getCount() {
        return mAlGeneralUserTabInUserManagement.size();
    }

    @Override
    public AllTabInUserManagementItem getItem(int position) {
        return mAlGeneralUserTabInUserManagement.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int pos, View convertView, ViewGroup parent) {
        // Initialize variables
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(textViewResourceId, null);

            viewHolder.circleIvItem = (CircleImageView) convertView.findViewById(
                    R.id.circleiv_item_in_simple_list_item_in_general_user_tab_in_user_management_fragment);
            viewHolder.mIbtnRemoveUser = (ImageButton) convertView.findViewById(
                    R.id.ibtn_remove_user_in_simple_list_item_in_general_user_tab_in_user_management_fragment);
            viewHolder.mIvPhoto = (ImageView) convertView.findViewById(
                    R.id.iv_photo_in_simple_list_item_in_general_user_tab_in_user_management_fragment);
            viewHolder.mIvVideo = (ImageView) convertView.findViewById(
                    R.id.iv_video_in_simple_list_item_in_general_user_tab_in_user_management_fragment);
            viewHolder.mLLPendingUser = (LinearLayout) convertView.findViewById(
                    R.id.ll_pending_user_in_in_simple_list_item_in_general_user_tab_in_user_management_fragment);
            viewHolder.mLLPhotosAndVideos = (LinearLayout) convertView.findViewById(
                    R.id.ll_photo_and_video_in_in_simple_list_item_in_general_user_tab_in_user_management_fragment);
            viewHolder.mTvMemberName = (TextView) convertView.findViewById(
                    R.id.tv_member_name_in_simple_list_item_in_general_user_tab_in_user_management_fragment);
            viewHolder.mTvPhotoNumber = (TextView) convertView.findViewById(
                    R.id.tv_photo_number_in_simple_list_item_in_general_user_tab_in_user_management_fragment);
            viewHolder.mTvVideoNumber = (TextView) convertView.findViewById(
                    R.id.tv_video_number_in_simple_list_item_in_general_user_tab_in_user_management_fragment);

            viewHolder.circleIvItem.setTag(pos);
            viewHolder.mIbtnRemoveUser.setTag(pos);
            viewHolder.mIvPhoto.setTag(pos);
            viewHolder.mIvVideo.setTag(pos);
            viewHolder.mLLPendingUser.setTag(pos);
            viewHolder.mLLPhotosAndVideos.setTag(pos);
            viewHolder.mTvMemberName.setTag(pos);
            viewHolder.mTvPhotoNumber.setTag(pos);
            viewHolder.mTvVideoNumber.setTag(pos);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

            viewHolder.circleIvItem.setTag(pos);
            viewHolder.mIbtnRemoveUser.setTag(pos);
            viewHolder.mIvPhoto.setTag(pos);
            viewHolder.mIvVideo.setTag(pos);
            viewHolder.mLLPendingUser.setTag(pos);
            viewHolder.mLLPhotosAndVideos.setTag(pos);
            viewHolder.mTvMemberName.setTag(pos);
            viewHolder.mTvPhotoNumber.setTag(pos);
            viewHolder.mTvVideoNumber.setTag(pos);
        }
        //set data
        try {
            String URL = "";
            if (API.IS_DEV_SITE)
                URL = mAlGeneralUserTabInUserManagement.get(pos).getAvatar().replace("https", "http");
            else
                URL = mAlGeneralUserTabInUserManagement.get(pos).getAvatar();

            Enterprise.imageLoader.displayImage(
                    URL,
                    viewHolder.circleIvItem, mDio,
                    new SimpleImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String imageUri, View view) {
                        }

                        @Override
                        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                        }

                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                            if (loadedImage != null & view.findViewWithTag(pos) != null) {
                                ((ImageView) view.findViewWithTag(pos)).setImageBitmap(loadedImage);
                            }
                        }
                    }, new ImageLoadingProgressListener() {
                        @Override
                        public void onProgressUpdate(
                                String imageUri, View view, int current, int total) {
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (!"null".equals(mAlGeneralUserTabInUserManagement.get(pos).getFirstName()) &&
                !"null".equals(mAlGeneralUserTabInUserManagement.get(pos).getLastName())) {
            viewHolder.mTvMemberName.setText(
                    mAlGeneralUserTabInUserManagement.get(pos).getFirstName() + " " +
                            mAlGeneralUserTabInUserManagement.get(pos).getLastName());
        } else {
            viewHolder.mTvMemberName.setText(
                    mAlGeneralUserTabInUserManagement.get(pos).getUserEmail()
            );
        }

        if (mAlGeneralUserTabInUserManagement.get(pos).getUserStatus().equals(
                mContext.getString(R.string.joined))) {
            //case user status is joined
            viewHolder.mLLPhotosAndVideos.setVisibility(View.VISIBLE);
            viewHolder.mLLPendingUser.setVisibility(View.GONE);
            viewHolder.mIbtnRemoveUser.setVisibility(View.INVISIBLE);

            if (mAlGeneralUserTabInUserManagement.get(pos).getNumberOfPhotos() > 1)
                viewHolder.mTvPhotoNumber.setText(
                        String.valueOf(mAlGeneralUserTabInUserManagement.get(pos).getNumberOfPhotos()) + " " +
                                mContext.getString(R.string.photos_));
            else

                viewHolder.mTvPhotoNumber.setText(
                        String.valueOf(mAlGeneralUserTabInUserManagement.get(pos).getNumberOfPhotos()) + " " +
                                mContext.getString(R.string.photo_));
            if (mAlGeneralUserTabInUserManagement.get(pos).getNumberOfVideos() > 1)
                viewHolder.mTvVideoNumber.setText(
                        String.valueOf(mAlGeneralUserTabInUserManagement.get(pos).getNumberOfVideos()) + " " +
                                mContext.getString(R.string.videos_));
            else
                viewHolder.mTvVideoNumber.setText(
                        String.valueOf(mAlGeneralUserTabInUserManagement.get(pos).getNumberOfVideos()) + " " +
                                mContext.getString(R.string.video_));
        } else {
            //case user status is waiting
            viewHolder.mLLPhotosAndVideos.setVisibility(View.GONE);
            viewHolder.mLLPendingUser.setVisibility(View.VISIBLE);
            viewHolder.mIbtnRemoveUser.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    /**
     * Basic methods
     */
}
